<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="600px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="选择客满专员" prop="KefuStaffID" label-width="120">
        <el-select v-model="form.KefuStaffID">
          <el-option label="请选择" :value="0" />
          <el-option
            v-for="(item, index) in KefuOptions"
            :key="index"
            :label="
              item.StaffName +
              '：（客户数量' +
              item.CustomerCount +
              '，行业：' +
              item.IndustryTexts +
              '）'
            "
            :value="item.StaffID"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { getKefuOptions } from '@/api/common'
  import { saveKefuSet } from '@/api/business'

  export default defineComponent({
    name: 'BusinessSatisfactionAdminEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        form: {},
        rules: {
          KefuStaffID: [
            { required: true, trigger: 'blur', message: '请选择客满专员' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        KefuOptions: [],
      })

      const showEdit = (row) => {
        // if (!row) {
        //   state.title = '添加'
        // } else {
        //   state.title = '编辑'
        //   state.form = Object.assign({}, row)
        // }
        console.log(row, 'WorkTaskIDs')
        state.form.WorkTaskIDs = row
        getKefuOption()
        state.dialogFormVisible = true
      }

      const getKefuOption = async () => {
        const {
          data: { StaffOptions },
        } = await getKefuOptions()
        state.KefuOptions = StaffOptions
      }

      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }

      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await saveKefuSet(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      return {
        ...toRefs(state),
        showEdit,
        getKefuOption,
        close,
        save,
      }
    },
  })
</script>
